package com.chatsocket.socketServlet;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.apache.log4j.Logger;

import com.chatsocket.util.Constant;
@ServerEndpoint(value="/websocket2/{userRemark}/{sessionId}",configurator=HttpSocketConfig.class)
public class WebSocketServlet2 extends WebSocketServlet{
	private Logger logger=Logger.getLogger(this.getClass());
    private Session session;
    //当前用户标记
    private String userRemark;
    private String httpSessionId;
    /**
     * 连接建立成功调用的方法
     * @param session  可选的参数。session为与某个客户端的连接会话，需要通过它来给客户端发送数据
     */
    @OnOpen
    public void onOpen(Session session,@PathParam("userRemark") String userRemark,@PathParam("sessionId") String sessionId,EndpointConfig config){
    	setHttpSessionId(sessionId);
        setSession(session);
        setUserRemark(userRemark);
        CopyOnWriteArraySet<WebSocketServlet> sockets = Constant.CURRENT_ONLINE_WEBSOCKET.get(userRemark);
        if(sockets==null||sockets.size()==0){
        	sockets=new CopyOnWriteArraySet<WebSocketServlet>();
        }
        sockets.add(this);
        Constant.CURRENT_ONLINE_WEBSOCKET.put(userRemark,sockets );
        logger.info("用户:"+userRemark+" 登陆人数为["+sockets.size()+"]人");
    }
     
    /**
     * 连接关闭调用的方法
     * 从webSocket池中删除该webSocket
     */
    @OnClose
    public void onClose(){
    	System.out.println("dkkkkkkkkkkkkkk");
    	Constant.CURRENT_ONLINE_WEBSOCKET.get(userRemark).remove(this);
    }
     
    /**
     * 收到客户端消息后调用的方法
     * @param message 客户端发送过来的消息
     * @param session 可选的参数
     */
    @OnMessage
    public void onMessage(String message, Session session) {
    }
    /**
     * 发生错误时调用
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error){
        error.printStackTrace();
    }
    /**
     * 这个方法与上面几个方法不一样。没有用注解，是根据自己需要添加的方法。
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException{
        this.session.getBasicRemote().sendText(message);
    }
	public String getHttpSessionId() {
		return httpSessionId;
	}

	public void setHttpSessionId(String httpSessionId) {
		this.httpSessionId = httpSessionId;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public String getUserRemark() {
		return userRemark;
	}

	public void setUserRemark(String userRemark) {
		this.userRemark = userRemark;
	}
	/**
	 * 用于从缓存中删除webSocket 
	 */
	@Override
	public boolean equals(Object obj) {
		if(this.httpSessionId==null||obj==null||(!(obj instanceof WebSocketServlet2))){
			return false;
		}
		if(this.getHttpSessionId().equals(((WebSocketServlet2)obj).getHttpSessionId())){
			return true;
		}
		return false;
	}
}
